package a07_二叉树;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * a02_二叉树的递归遍历复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/5/20
 */
public class a02_二叉树的递归遍历复习4 {
    //中左右
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        preorderTraversal(root,list);
        return list;
    }

    public void preorderTraversal(TreeNode node,List<Integer> list) {
        if (node == null) {
            return;
        }
        list.add(node.val);
        preorderTraversal(node.left,list);
        preorderTraversal(node.right,list);
    }

    //左中右
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        inorderTraversal(root,list);
        return list;
    }

    public void inorderTraversal(TreeNode node,List<Integer> list) {
        if (node == null) {
            return;
        }
        inorderTraversal(node.left,list);
        list.add(node.val);
        inorderTraversal(node.right,list);
    }

    //左右中
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        postorderTraversal(root,list);
        return list;
    }

    public void postorderTraversal(TreeNode node,List<Integer> list) {
        if (node == null) {
            return;
        }
        postorderTraversal(node.left,list);
        postorderTraversal(node.right,list);
        list.add(node.val);
    }
}
